package com.soujiayi.share;

import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;

import org.json.JSONObject;


import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;

import com.soujiayi.activity.R;
import com.soujiayi.util.CommonUtils;
import com.soujiayi.util.Constants;
import com.soujiayi.weibo.User;
import com.weibo.sdk.android.Oauth2AccessToken;
import com.weibo.sdk.android.Weibo;
import com.weibo.sdk.android.WeiboAuthListener;
import com.weibo.sdk.android.WeiboDialogError;
import com.weibo.sdk.android.WeiboException;
import com.weibo.sdk.android.api.StatusesAPI;
import com.weibo.sdk.android.net.RequestListener;

public class SinaWeiboAPI {

	private Weibo mWeibo;

	private Oauth2AccessToken accessToken;

	private Context context;
	private Handler handler;

	public SinaWeiboAPI(Context context, Handler handler) {
		this.context = context;
		this.handler = handler;
		mWeibo = Weibo.getInstance(Constants.SINA_CONSUMER_KEY, Constants.SINA_REDIRECT_URL);
	}

	public void login() {
		mWeibo.authorize(context, new AuthDialogListener());
	}

//	public void send(String content, Oauth2AccessToken token) {
//		StatusesAPI statusesAPI = new StatusesAPI(token);
////		String picPath = Environment.getExternalStorageDirectory() +  File.separator + Constants.STORAGECARD_DIR + File.separator + "logo_icon.png";
////		CommonUtils.copyAppPicToSDCard(context, R.drawable.ic_launcher, picPath);
//		statusesAPI.uploadUrlText(URLEncoder.encode(content), picPath, null, null, new RequestListener() {
////		statusesAPI.update(content, null, null, new RequestListener() {
//
//			@Override
//			public void onIOException(IOException e){ 
//				Message msg = new Message();
//				Bundle bundle = new Bundle();
//				bundle.putInt("api_type", 1);
//				bundle.putInt("function_type", 2);
//				bundle.putBoolean("isSuccess", false);
//				msg.setData(bundle);
//				handler.sendMessage(msg);
//				e.printStackTrace();
//			}
//
//			@Override
//			public void onError(WeiboException e) {
//				e.printStackTrace();
//				boolean shareResult = false;
//				try{
//					String result = e.toString().substring(e.toString().indexOf("{"));
//					System.out.println(result);
//					JSONObject obj = new JSONObject(result);
//					int error_code = obj.getInt("error_code");
//					if(error_code == 20019){
//						shareResult = true;
//					}
//				}catch(Exception e2){
//					e.printStackTrace();
//				}
//				Message msg = new Message();
//				Bundle bundle = new Bundle();
//				bundle.putInt("api_type", 1);
//				bundle.putInt("function_type", 2);
//				bundle.putBoolean("isSuccess", shareResult);
//				msg.setData(bundle);
//				handler.sendMessage(msg);
//			}
//
//			@Override
//			public void onComplete(String response) {
//				Message msg = new Message();
//				Bundle bundle = new Bundle();
//				bundle.putInt("api_type", 1);
//				bundle.putInt("function_type", 2);
//				bundle.putBoolean("isSuccess", true);
//				msg.setData(bundle);
//				handler.sendMessage(msg);
//			}
//		});
//	}
	
	public void send(String content, Oauth2AccessToken token, String imageUrl) {
//		boolean isImageExist = false;
//		String picPath = "";
//		if(imageUrl != null && !imageUrl.equals("")){
//			picPath = Environment.getExternalStorageDirectory() +  File.separator + Constants.CACHE_DIR + File.separator + String.valueOf(imageUrl.hashCode());
//			File f = new File(picPath);
//			if(f.exists()){
//				isImageExist = true;
//			}
//		}
//		if(!isImageExist){
//			update(content, token);
//			return;
//		}
		StatusesAPI statusesAPI = new StatusesAPI(token);
		statusesAPI.uploadUrlText(content, imageUrl, null, null, new RequestListener() {

			@Override
			public void onIOException(IOException e){ 
				Message msg = new Message();
				Bundle bundle = new Bundle();
				bundle.putInt("api_type", 1);
				bundle.putInt("function_type", 2);
				bundle.putBoolean("isSuccess", false);
				msg.setData(bundle);
				handler.sendMessage(msg);
			}

			@Override
			public void onError(WeiboException e) {
				boolean shareResult = false;
				try{
					String result = e.toString().substring(e.toString().indexOf("{"));
					System.out.println(result);
					JSONObject obj = new JSONObject(result);
					int error_code = obj.getInt("error_code");
					if(error_code == 20019){
						shareResult = true;
					}
				}catch(Exception e2){
					
				}
				Message msg = new Message();
				Bundle bundle = new Bundle();
				bundle.putInt("api_type", 1);
				bundle.putInt("function_type", 2);
				bundle.putBoolean("isSuccess", shareResult);
				msg.setData(bundle);
				handler.sendMessage(msg);
			}

			@Override
			public void onComplete(String response) {
				Message msg = new Message();
				Bundle bundle = new Bundle();
				bundle.putInt("api_type", 1);
				bundle.putInt("function_type", 2);
				bundle.putBoolean("isSuccess", true);
				msg.setData(bundle);
				handler.sendMessage(msg);
			}
		});
	}
	
	public void update(String content, Oauth2AccessToken token) {
		StatusesAPI statusesAPI = new StatusesAPI(token);
		statusesAPI.update(content, null, null, new RequestListener() {
			@Override
			public void onIOException(IOException e){ 
				Message msg = new Message();
				Bundle bundle = new Bundle();
				bundle.putInt("api_type", 1);
				bundle.putInt("function_type", 2);
				bundle.putBoolean("isSuccess", false);
				msg.setData(bundle);
				handler.sendMessage(msg);
			}

			@Override
			public void onError(WeiboException e) {
				boolean shareResult = false;
				try{
					String result = e.toString().substring(e.toString().indexOf("{"));
					System.out.println(result);
					JSONObject obj = new JSONObject(result);
					int error_code = obj.getInt("error_code");
					if(error_code == 20019){
						shareResult = true;
					}
				}catch(Exception e2){
					
				}
				Message msg = new Message();
				Bundle bundle = new Bundle();
				bundle.putInt("api_type", 1);
				bundle.putInt("function_type", 2);
				bundle.putBoolean("isSuccess", shareResult);
				msg.setData(bundle);
				handler.sendMessage(msg);
			}

			@Override
			public void onComplete(String response) {
				Message msg = new Message();
				Bundle bundle = new Bundle();
				bundle.putInt("api_type", 1);
				bundle.putInt("function_type", 2);
				bundle.putBoolean("isSuccess", true);
				msg.setData(bundle);
				handler.sendMessage(msg);
			}
		});
	}

	class AuthDialogListener implements WeiboAuthListener {
		@Override
		public void onComplete(Bundle values) {
			String token = values.getString("access_token");
			String expires_in = values.getString("expires_in");
			accessToken = new Oauth2AccessToken(token, expires_in);
			if (accessToken.isSessionValid()) {
				User user = new User();
				user.setType(Constants.USER_TYPE_SINA);
				user.setSina_token(token);
				user.setSina_expires_in(expires_in);
				user.setSina_expires_time(String.valueOf(accessToken.getExpiresTime()*1000));
				Message msg = new Message();
				Bundle bundle = new Bundle();
				bundle.putInt("api_type", 1);
				bundle.putInt("function_type", 1);
				bundle.putBoolean("isSuccess", true);
				bundle.putSerializable(Constants.USER, user);;
				msg.setData(bundle);
				handler.sendMessage(msg);
			}
		}

		@Override
		public void onError(WeiboDialogError e) {
			Message msg = new Message();
			Bundle bundle = new Bundle();
			bundle.putInt("api_type", 1);
			bundle.putInt("function_type", 1);
			bundle.putBoolean("isSuccess", false);
			msg.setData(bundle);
			handler.sendMessage(msg);
		}

		@Override
		public void onCancel() {
			//Toast.makeText(context, "Auth cancel", Toast.LENGTH_LONG).show();
		}

		@Override
		public void onWeiboException(WeiboException e) {
			Message msg = new Message();
			Bundle bundle = new Bundle();
			bundle.putInt("api_type", 1);
			bundle.putInt("function_type", 1);
			bundle.putBoolean("isSuccess", false);
			msg.setData(bundle);
			handler.sendMessage(msg);
		}
	}

}
